<?php namespace Admin\Controller;
use Hdphp\Controller\Controller;
use Common\Model\Goodslist;
//货品列表控制器
class GoodslistController extends CommonController{
	private $model;
	private $c;
	public function __init(){
	    /*//调用CommonController里面的构造方法__init()(包含验证是否登录)*/
	    parent::__init();
		$this->model= new \Common\Model\Goodsattr;
		$this->c = new \Common\Model\Goodslist;
	}
	//货品列表展示
	public function index(){
		//1显示首页
		//获得所有数据
		$gid=Q('get.gid',0,'intval');
		//关联商品属性表和类型属性表
		$data = $this->model->join("typeattr","typeattr_taid","=","taid")->where("goods_gid={$gid}")->where("class=1")->get();
		View::with('data',$data);
		//显示货品列表调用
		$goodslistdata = $this->c->get();
		//获取货品列表的数据
		$data1 = $this->c->where("goods_gid={$gid}")->get();
		foreach ($data1 as $k => $v) {
			$v['combine']=explode('|', $v['combine']);
			$data1[$k]['combine']=$v['combine'];
			foreach ($v['combine'] as $kk => $vv) {
				//获得对应的goods_gid的那一条数据
				$gtvalue=$this->model->where("gaid={$vv}")->field('gtvalue')->find();
				$data1[$k]['combine'][$kk]=$gtvalue['gtvalue'];
			}
		}
		View::with('data1',$data1);
		//2添加货品数据
		$arr=array();
		foreach ($data as $k => $v) {
			$arr[$v['taid']][]=$v;
		}
//		p($arr);
		View::with('arr',$arr);
		if(IS_POST){
			$gid = Q('post.goods_gid',0,'intval');
			//获取所有post数据
			$data= Q('post.');
//			p($data);exit;
			//把数组转化为字符串
			$data['combine'] = implode('|', $data['combine']);
			$duibi = $this->c->where("combine='{$data['combine']}'")->get();
			//加判断,如果已存在,则不能添加数据
			if($duibi){
				View::error('数据已存在,请重新添加',U('index',array("gid"=>$gid)));

			}else{
				$this->c->add($data);
				View::success('添加成功',U('index',array("gid"=>$gid)));
			}
		}
	    View::make();
	}
	//修改货品
	public function edit(){
		$gid=Q('get.gid',0,'intval');
		if(IS_POST){
			$glid=Q('post.glid',0,'intval');
			if(!$this->c->edit()) View::error($this->c->getError());
			View::success('编辑成功',U('index',array('gid'=>$gid)));
		}
		//1处理所属下拉数据
		$gid=Q('get.gid',0,'intval');
		//关联商品属性表和类型属性表
		$data = $this->model->join("typeattr","typeattr_taid","=","taid")->where("goods_gid={$gid}")->where("class=1")->get();
		//分配数据
		View::with('data',$data);
		//2添加货品数据
		$arr=array();
		foreach ($data as $k => $v) {
			$arr[$v['taid']][]=$v;
		}
		View::with('arr',$arr);
		//3获得旧数据
		$glid=Q('get.glid',0,'intval');
		$oldData=$this->c->where("glid='{$glid}'")->find();
		
		//把字符串转化为数组
		$oldData['combine']=explode('|', $oldData['combine']);
		View::with('oldData',$oldData);
	    View::make();
	}
	//删除货品
	public function del(){
		//接收cid
		$glid = Q('get.glid',0,'intval');
		//3.删除对应的货品
		$Glistmodel = new \Common\Model\Goodslist;
	    $Glistmodel->where("glid=$glid")->delete();
		View::success('删除成功');
	}
	
	
}


 ?>